package com.cyandpos.pad.pay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.List;

public class GoodsAdapter extends BaseAdapter {
    private Context context;
    private List<Goods> list;

    public GoodsAdapter(Context context,List<Goods> list){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Goods getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_goods,null);
            holder=new Holder();
            holder.tv_goods_number=convertView.findViewById(R.id.tv_goods_number);
            holder.tv_goods_name=convertView.findViewById(R.id.tv_goods_name);
            holder.tv_goods_price=convertView.findViewById(R.id.tv_goods_price);
            holder.tv_goods_sums=convertView.findViewById(R.id.tv_goods_sums);
            holder.tv_goods_discount=convertView.findViewById(R.id.tv_goods_discount);
            holder.tv_goods_money=convertView.findViewById(R.id.tv_goods_money);
            convertView.setTag(holder);
        }else{
            holder= (Holder) convertView.getTag();
        }
        Goods goods=getItem(position);
        if(goods!=null){
            int ticktGoodsInx =1;
            int ticktInx =1;
            if(goods.getTicktGoodsInx()!=null && goods.getTicktInx()!=null){
                ticktGoodsInx = Integer.parseInt(goods.getTicktGoodsInx());
                ticktInx = Integer.parseInt(goods.getTicktInx());
            }
            holder.tv_goods_number.setText((ticktGoodsInx+1)+"/"+(ticktInx+1));
            holder.tv_goods_name.setText(goods.getName());
            holder.tv_goods_price.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(goods.getPrice())));
            if (ErpCommonData.ticketInfo.getOldOrderNo().length() > 0){
                holder.tv_goods_sums.setText(Math.abs(Integer.parseInt(goods.getRefundCount()))+"");
            }else {
                holder.tv_goods_sums.setText(Math.abs(Integer.parseInt(goods.getSaleCount()))+"");
            }
            holder.tv_goods_discount.setText(MoneyUtils.getMabMoneyString(goods.getDiscount()));//折扣
            holder.tv_goods_money.setText(MoneyUtils.getMabMoneyString(MoneyUtils.getSubtractMoney(goods.getSaleMoney(),goods.getDiscount()).toString()));
        }
        return convertView;
    }

    class Holder{
        private TextView tv_goods_number;
        private TextView tv_goods_name;
        private TextView tv_goods_price;
        private TextView tv_goods_sums;
        private TextView tv_goods_discount;
        private TextView tv_goods_money;
    }
}
